var express = require('express');
const musr = require('./users');
const cman = require('../wssclnt');
var router = express.Router();

router.use((req, res, next) => {
    if (musr.isLogined(req)) {
        next();
    } else {
        res.send({ "success": false, "err": "NO_LOGIN" });
    }
});

router.get("/connect", function(req,res,next){
    res.send({"success":cman.connect(),"err":""});
});

router.get("/disconnect", function(req,res,next){
    cman.disconnect();
    res.send({"success":true,"err":""});
});

router.get("/push_start", function(req,res,next){
    cman.startPush();
    res.send({"success":true,"err":""});
});

router.get("/push_stop", function(req,res,next){
    cman.stopPush();
    res.send({"success":true,"err":""});
});

router.get("/status",function(req,res,next){
    res.send({
        "connected": cman.isConnected(),
        "pushing": cman.isPushing()
    })
});
router.post('/general', function (req, res, next) {
    if ("server" in req.body && "pusher" in req.body && "id" in req.body && "key" in req.body) {
        cman.server = req.body.server;
        cman.pusher_cmd = req.body.pusher;
        cman.id = req.body.id;
        cman.key = req.body.key;
        res.send({ "success": cman.saveConfig(), "err": "" });
    } else {
        res.send({ "success": false, "err": "empty" });
    }

});

module.exports = router;
